package com.zzhardy.controller.admin.permission.service.impl;

import java.util.Arrays;
import java.util.Date;
import java.util.List;

import com.zzhardy.common.model.ResultResponse;
import com.zzhardy.controller.admin.permission.dao.SysMenuDao;
import com.zzhardy.controller.admin.permission.model.SysMenu;
import com.zzhardy.controller.admin.permission.model.enums.SysMenuEnums;
import com.zzhardy.controller.admin.permission.service.SysMenuService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * @Author：Hardy
 * @Date：2021/1/3
 * @Function:
 */
@Service
public class SysMenuServiceImpl implements SysMenuService {
    
    @Autowired
    private SysMenuDao sysMenuDao;
    
    /**
     * 新增或者修改菜单信息
     */
    @Override
    public ResultResponse saveOrUpdateMenu(SysMenu sysMenu, int uid, int unitId, int sysId) {
        sysMenu.setUnitId(unitId);
        sysMenu.setSysId(sysId);
        sysMenu.setState(SysMenuEnums.STATE_USE);
        // 设置父节点与节点长度
        if (sysMenu.getParentId() == null || sysMenu.getParentId() == 0) {
            sysMenu.setParentId(SysMenuEnums.PARENT_ID);
            sysMenu.setIsLeaf(SysMenuEnums.IS_LEAF);
            sysMenu.setLevel(SysMenuEnums.LEVEL);
        } else {
            SysMenu parent = sysMenuDao.single(sysMenu.getParentId());
            if (null != parent) {
                parent.setIsLeaf(SysMenuEnums.IS_LEAF);
                sysMenu.setLevel(parent.getLevel() + 1);
                sysMenuDao.updateTemplateById(parent);
            } else {
                sysMenu.setLevel(SysMenuEnums.LEVEL);
            }
        }
        if (sysMenu.getId() == null || sysMenu.getId() == 0) {
            sysMenu.setCreateTime(new Date());
            sysMenu.setCreateId(uid);
            sysMenu.setIsLeaf(SysMenuEnums.IS_NOT_LEAF);
            sysMenuDao.insert(sysMenu);
            return ResultResponse.success("保存成功！");
        } else {
            sysMenu.setUpdateTime(new Date());
            sysMenu.setUpdateId(uid);
            sysMenuDao.updateTemplateById(sysMenu);
            return ResultResponse.success("修改成功！");
        }
    }
    
    
    /**
     * 获取指定机构单元下的所有菜单
     */
    @Override
    public List<SysMenu> getAllMenuByUnitIdAndSysId(int unitId, int sysId) {
        List<SysMenu> parentData = sysMenuDao.getMenuByLevelAndParentId(unitId,
                                                                        sysId,
                                                                        SysMenuEnums.LEVEL,
                                                                        SysMenuEnums.PARENT_ID,
                                                                        SysMenuEnums.STATE_USE);
        parentData.stream().forEach(param -> {
            if (param.getIsLeaf() == 1) {
                getAllChildMenu(unitId, sysId, param);
            }
        });
        return parentData;
    }
    
    /**
     * 获取系统下，单元的所有数据
     */
    public void getAllChildMenu(int unitId, int sysId, SysMenu parentMenu) {
        List<SysMenu> children = sysMenuDao.getMenuByLevelAndParentId(unitId,
                                                                      sysId,
                                                                      parentMenu.getLevel() + 1,
                                                                      parentMenu.getId(),
                                                                      SysMenuEnums.STATE_USE);
        children.stream().forEach(param -> {
            if (null != param.getIsLeaf() && param.getIsLeaf() == 1) {
                getAllChildMenu(unitId, sysId, param);
            }
        });
        parentMenu.setChildren(children);
    }
    
    /**
     * 逻辑删除菜单数据
     */
    public void deleteMenuById(int menuId, int uid) {
        SysMenu deleteData = sysMenuDao.single(menuId);
        if (deleteData.getIsLeaf() == 1) {
            List<SysMenu> menuList = sysMenuDao.getMenuByParentId(deleteData.getId());
            menuList.stream().forEach(param -> {
                param.setState(-1);
                param.setUpdateId(uid);
                param.setUpdateTime(new Date());
                sysMenuDao.updateTemplateById(param);
            });
        }
        deleteData.setUpdateTime(new Date());
        deleteData.setState(-1);
        deleteData.setUpdateId(uid);
        sysMenuDao.updateTemplateById(deleteData);
    }
    
    /**
     * 批量逻辑删除菜单数据
     */
    @Override
    public void batchDeleteMenu(String menuStrList, int uid) {
        if (StringUtils.isNotBlank(menuStrList)) {
            String[] menuListArr = menuStrList.split(",");
            List<String> menuList = Arrays.asList(menuListArr);
            menuList.stream().forEach(param -> deleteMenuById(Integer.valueOf(param), uid));
        }
    }
}
